<?php

/* @project : Template
 * @auther  : 青云
 * @date    : 2018/1/10
 * @desc    : 系统配置控制器
 */

namespace app\admin\controller;

use app\common\model\Mail;
use app\common\model\SystemConfig as SystemConfigModel;
use app\common\model\SystemConfigTab as SystemConfigTabModel;
use think\Request;

class SystemConfig extends Base
{
    public function add()
    {
        return $this->fetch();
    }

    // 选择TAB分类
    public function select_config_tab()
    {
        $where_menu[] = array('is_del', 'eq', 0);
        $menu_list = (new SystemConfigTabModel)->field('id,0 pid,name,sort')->where($where_menu)->order('sort asc')->select();

        $root['id'] = 0;
        $root['name'] = '根节点';

        if (!$menu_list->isEmpty()) {
            $menu_tree = list_to_tree($menu_list->toArray());
            $root['_child'] = $menu_tree;
        }

        $this->assign('menu_tree', $menu_tree);

        return $this->fetch();
    }

    public function admin(Request $request)
    {
        $request->eq_config_tab_id = $request->config_tab_id;
        $request->_sort = 'sort asc';
        return $this->_index($request);
    }

    public function set_valid(Request $request)
    {
        return $this->_set_valid($request);
    }

    public function set_invalid(Request $request)
    {
        return $this->_set_invalid($request);
    }

    public function _empty(Request $request)
    {
        $data_type = $request->action();

        $config_tab_list = (new SystemConfigTabModel)->getListByType($data_type);
        $this->assign('config_tab_list', $config_tab_list);

        $tab_id = $request->tab_id;
        if (empty($tab_id)) {
            $tab_id = $config_tab_list->toArray()[0]['id'];
        }
        $this->assign('tab_id', $tab_id);

        $field_list = (new SystemConfigModel)->getFieldListByTabId($tab_id);

        $this->assign('field_list', $field_list);
        return $this->fetch('config');
    }

    public function save_field(Request $request)
    {
        //data_type 划分：
        $data = $request->param();

        foreach ($data as $key => $val) {
                $this->set_val($key, $val);            
        }
        return $this->api_success('保存成功');
    }

    function del(Request $request){
        $ret= $this->_destory($request);
        if($ret){
            return $this->api_success('删除成功');
        }else{
            return $this->api_error('删除失败');
        }
    }

    //-------------
    public function captcha(Request $request)
    {
        $where_system[] = array('data_type', 'eq', 'captcha');
        $system_data = model('SystemConfig')->where($where_system)->column('val', 'code');
        $this->assign('system_data', $system_data);

        return $this->fetch();
    }

    public function setting(Request $request)
    {
        $where_system[] = array('data_type', 'eq', 'system');
        $system_data = model('SystemConfig')->where($where_system)->column('val', 'code');
        $this->assign('system_data', $system_data);

        return $this->fetch();
    }

    public function zhifubao(Request $request)
    {
        $where_system[] = array('data_type', 'eq', 'zhifubao');
        $system_data = model('SystemConfig')->where($where_system)->column('val', 'code');
        $this->assign('system_data', $system_data);

        return $this->fetch();
    }

    public function order(Request $request)
    {
        $where_system[] = array('data_type', 'eq', 'order');
        $system_data = model('SystemConfig')->where($where_system)->column('val', 'code');
        $this->assign('system_data', $system_data);

        return $this->fetch();
    }

    public function security(Request $request)
    {
        $where_system[] = array('data_type', 'eq', 'security');
        $system_data = model('SystemConfig')->where($where_system)->column('val', 'code');
        $this->assign('system_data', $system_data);

        return $this->fetch();
    }

    public function bus(Request $request)
    {
        $where_system[] = array('data_type', 'eq', 'bus');
        $system_data = model('SystemConfig')->where($where_system)->column('val', 'code');
        $this->assign('system_data', $system_data);

        return $this->fetch();
    }

    public function bottom(Request $request)
    {
        $where_system[] = array('data_type', 'eq', 'bottom');
        $system_data = model('SystemConfig')->where($where_system)->column('val', 'code');
        $this->assign('system_data', $system_data);

        return $this->fetch();
    }

    public function visit(Request $request)
    {
        $where_system[] = array('data_type', 'eq', 'visit');
        $system_data = model('SystemConfig')->where($where_system)->column('val', 'code');
        $this->assign('system_data', $system_data);

        return $this->fetch();
    }

    public function site(Request $request)
    {
        $where_system[] = array('data_type', 'eq', 'site');
        $system_data = model('SystemConfig')->where($where_system)->column('val', 'code');
        $this->assign('system_data', $system_data);

        return $this->fetch();
    }

    public function index(Request $request)
    {

        $where_system[] = array('data_type', 'eq', 'system');
        $system_data = model('SystemConfig')->where($where_system)->column('val', 'code');
        $this->assign('system_data', $system_data);

        return $this->fetch();
    }

    public function email(Request $request)
    {

        $where_system[] = array('data_type', 'eq', 'system');
        $system_data = model('SystemConfig')->where($where_system)->column('val', 'code');
        $this->assign('system_data', $system_data);

        return $this->fetch();
    }

    public function qiniu(Request $request)
    {
        $where_system[] = array('data_type', 'eq', 'system');
        $system_data = model('SystemConfig')->where($where_system)->column('val', 'code');
        $this->assign('system_data', $system_data);

        return $this->fetch();
    }

    public function weixin(Request $request)
    {

        $where_system[] = array('data_type', 'eq', 'weixin');
        $system_data = model('SystemConfig')->where($where_system)->column('val', 'code');
        $this->assign('system_data', $system_data);

        return $this->fetch();
    }

    function save(Request $request)
    {
        //data_type 划分：
        $data_type = $request->data_type;
        $data      = $request->param();

        foreach ($data as $key => $val) {
            if (strpos($key, $data_type . '_') == 0) {
                $this->set_val($key, $val, $data_type);
            }
        }
        return $this->api_success('保存成功');
    }

    protected function set_val($key, $val)
    {
        $data['val'] = $val;        
        $where_system[] = array('code', 'eq', $key);
        $vo = model('SystemConfig')->where($where_system)->find();
 
        if ($vo) {
            $data['id'] = $vo['id'];
            $list = $vo->data($data)->save();
        } else {
            $data['code'] = $key;
            $list = model('SystemConfig')->insert($data);
        }
    }

    public function check_email(Request $request)
    {
        if ($request->isAjax()) {
            $mail_model = new Mail();
            $mail_model->send($request->to_email, $request->subject, $request->content);
            return $this->api_success('邮件已发送');
        } else {
            return $this->fetch();
        }
    }

    public function upload()
    {
        return $this->_upload();
    }

    public function preview(Request $request)
    {
        cache('bottom_code', $request->bottom_code);
        return $this->api_success('保存成功');
    }

    public function download($sid)
    {
        $this->_download($sid);
    }

}
